<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('Entity/Doc_Comments_Entity.php');
class Doc_Comments_Model extends Doc_Comments_Entity
{
    public function __construct()
    {
        parent::__construct();
    }

    public function addComment()
    {
        $this->setFullname($this->input->post('fullname'));
        $this->setEmail($this->input->post('email'));
        $this->setContents($this->input->post('contents'));
        $this->setDid($this->input->post('vid'));
        $this->setCreated(time());
        $this->setChanged(time());
        $this->setStatus(0);
        return $this->save();
    }
    public function dataSort($field, $order)
    {
        $xOrder = $order == 'asc' ? 'desc' : 'asc';
        $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';
        $url = anchor('doc_comments/sort/' . $field . '/' . $xOrder, t($field) . $img);
        $_SESSION['sm_sort_doc_comments'] = array(
            'field' => $field,
            'order' => $order,
            'url' => $url);
    }

    public function dataIndex($target)
    {
        $numshow = 25;
        $this->db->select('doc_comments.id, doc_comments.fullname, doc_comments.email, doc_comments.status, doc_comments.created, doc_documents.title as did_type');
        $this->db->from('doc_comments');

        $this->db->join('doc_documents', 'doc_comments.did = doc_documents.id');

        if ($this->session->userdata('sm_filter_doc_documents'))
            $this->db->where('doc_comments.did', $this->session->userdata('sm_filter_doc_documents'));
        if ($this->session->userdata('sm_find_doc_comments')) {
            $this->db->like('doc_comments.contents', $this->session->userdata('sm_find_doc_comments'));
        }
        if (isset($_SESSION['sm_sort_doc_comments'])) {
            $sort = $_SESSION['sm_sort_doc_comments'];
            $this->db->order_by('doc_comments.' . $sort['field'], $sort['order']);
        } else {
            $this->db->order_by('doc_comments.created', 'desc');
        }
        $this->db->limit($numshow, $target);
        $query = $this->db->get();
        $paging = paging_admin('doc_comments/index', $this->_total(), $numshow, 4);
        return array(
            'query' => $query,
            'paging' => $paging,
            'fil_doc_documents' => $this->Filter_doc_documents(),
            'formFind' => $this->formFind());
    }

    private function cbo_doc_documents($id = 0)
    {
        $this->load->helper("form");
        $query = $this->db->get("doc_documents");
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $sl = $id == 0 ? set_value("did") : $id;
        return form_dropdown("did", $options, $sl);
    }

    private function _total()
    {
        return $this->db->count_all_results("doc_comments");
    }

    private function _checkdoc_commentsById($id)
    {
        $query = $this->db->get_where("doc_comments", array("id" => $id));
        if ($query->num_rows() > 0)
            return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")), "doc_comments");
            return false;
        }
    }

    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('doc_comments/find'), $attributes);
        $data = array(
            'name' => 'txt_find',
            'id' => 'txt_find',
            'value' => '',
            'style' => 'width:200px');
        $f .= form_input($data);
        $s = array(
            'name' => 'submit',
            'value' => t('Find'),
            'class' => 'ibutton green',
            'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_doc_comments')) {
            $r = array(
                'name' => 'btnReset',
                'value' => 'Reset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }

    private function Filter_doc_documents()
    {
        $this->load->helper('form');
        $query = $this->db->get('doc_documents');
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $id = !$this->session->userdata('sm_filter_doc_documents') ? 0 : $this->session->userdata('sm_filter_doc_documents');
        $attributes = array('name' => 'frmFilterdoc_documents', 'id' => 'frmFilterdoc_documents');
        $f = form_open(site_url('doc_comments/filterdoc_documents'), $attributes);
        $f .= form_dropdown('did', $options, $id);
        $data = array(
            'name' => 'btnFilterdoc_documents',
            'class' => 'ibutton green',
            'style' => 'margin-left:10px',
            'value' => t('Filter'));
        $f .= form_submit($data);
        if ($this->session->userdata('sm_filter_doc_documents')) {
            $r = array(
                'name' => 'btnReset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px',
                'value' => 'Reset');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }


    public function dataEdit($id)
    {
        if (!$this->_checkdoc_commentsById($id))
            return redirect(site_url('doc_comments'));
        $query = $this->db->get_where('doc_comments', array('id' => $id));
        return array('query' => $query, 'cbo_doc_documents' => $this->cbo_doc_documents($query->row('did')));
    }

    public function dataAdd()
    {
        return array('cbo_doc_documents' => $this->cbo_doc_documents());
    }

    public function checkForm()
    {
        $this->form_validation->set_rules('fullname', t('fullname'), 'trim|required');
        $this->form_validation->set_rules('email', t('email'), 'trim|required');
        $this->form_validation->set_rules('contents', t('contents'), 'trim|required');
        $this->form_validation->set_rules('status', t('status'), 'trim|required|integer');
        $this->form_validation->set_rules('created', t('created'), 'trim|required|integer');
        $this->form_validation->set_rules('did', t('did'), 'trim|required|integer');
        return $this->form_validation->run();
    }

    public function doUpdate()
    {
        $this->load->helper('text');
        $this->setFullname($this->input->post('fullname'));
        $this->setEmail($this->input->post('email'));
        $this->setContents($this->input->post('contents'));
        $this->setStatus($this->input->post('status'));
        $this->setCreated($this->input->post('created'));
        $this->setDid($this->input->post('did'));

        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        } else {
            $this->setCreated(time());
        }
        return $this->save();
    }
}
